package com.be.beadmin.system.task;

import com.be.beadmin.common.exception.BeadminException;
import com.be.beadmin.product.entity.WorkOrderInfor;
import com.be.beadmin.product.repository.WorkOrderInforRepository;
import com.be.beadmin.quality.entity.InspectionTask;
import com.be.beadmin.quality.entity.InspectionTemplate;
import com.be.beadmin.quality.repository.InspectionTaskRepository;
import com.be.beadmin.quality.repository.InspectionTemplateRepository;
import com.be.beadmin.quality.service.InspectionTaskService;
import com.be.beadmin.service.task.BaseTask;
import com.be.beadmin.system.service.ProductMaterialService.WorkOrderInforPmService;
import com.be.beadmin.system.util.SpringUtil;

import java.util.List;

/**
 * @Description:
 * @author: Jiaming Zheng
 * @Date: Create in 20:22 2023/12/25
 * @Modified By:
 **/

public class ReleaseWoTask extends BaseTask {


    private final WorkOrderInforPmService workOrderInforPmService = SpringUtil.getBean(WorkOrderInforPmService.class);

    private final InspectionTaskRepository inspectionTaskRepository = SpringUtil.getBean(InspectionTaskRepository.class);

    private final InspectionTemplateRepository inspectionTemplateRepository = SpringUtil.getBean(InspectionTemplateRepository.class);

    private final InspectionTaskService inspectionTaskService = SpringUtil.getBean(InspectionTaskService.class);

    private final WorkOrderInforRepository workOrderInforRepository = SpringUtil.getBean(WorkOrderInforRepository.class);


    private WorkOrderInfor workOrderInfor;

    public ReleaseWoTask(WorkOrderInfor workOrderInfor) {
        super("释放工单生成终检单、交检单");
        this.workOrderInfor = workOrderInfor;
    }

    /**
     * @description 执行任务
     */
    @Override
    public void doTask() {

        // 释放工单
        workOrderInforPmService.releaseWorkOrderInfor(workOrderInfor.getId());
        // 生成终检单
        System.out.println("正在生成终检单");
        List<InspectionTask> taskList = inspectionTaskRepository.findInspectionTaskByWoAndTypeAndDelete(workOrderInfor.getWo(), "终检", 0);
        if (taskList.size() > 1) {
//                throw new BeadminException("终检单超过一个了");
        }
        if (taskList.size() == 0) { // 如果有的话就不生成了
            // 查找终检单模板
            List<InspectionTemplate> templatelist = inspectionTemplateRepository.findInspectionTemplateByStateAndDeleteAndType("1", 0, "终检");
            if (templatelist.size() == 0) throw new BeadminException("未找到已发布的终检检验表模板");
            // 生成终检单
            inspectionTaskService.generateFinishInspectionTask(workOrderInfor, templatelist.get(0));
        }
        // 生成交检单
//        // 检查工序自检单的模板是否存在, 所有工序， 需要自检的去检查其模板是否存在；
//        //
//        List<String> noSelfCheckTempleteProcess = workOrderInforRepository.findNoSelfCheckTempleteProcess(workOrderInfor.getWo(), workOrderInfor.getProductType());
//        if (noSelfCheckTempleteProcess.size()!=0) {
//            StringBuilder sb = new StringBuilder();
//            sb.append("排程失败，有工序没有自检模板。柜体类型：")
//                    .append(workOrderInfor.getProductType())
//                    .append("工序：")
//                    .append(String.join(",",noSelfCheckTempleteProcess));
//            throw new BeadminException("")
//        }


    }
}
